#include <REGX52.H>
#include "Timer0.h"
#include "motor.h"
  sbit ENA=P2^5;
	sbit IN1=P2^4;
	sbit IN2=P2^3;
	sbit IN3=P2^2;
	sbit IN4=P2^1;
	sbit ENB=P2^0;
unsigned char R_speed = 62;
unsigned char L_speed = 60;
unsigned char Rcount,Lcount;
unsigned char Rsave=62;//直行值
unsigned char Lsave=60;
unsigned char dcount,dcompare;
char CHA=0;


static unsigned int T0Count = 0; 
void car_go(){
	L_speed=Lsave;
	R_speed=Rsave;
	//EX0=1;EX1=1;
	L_motor_go();
	R_motor_go();
}
void car_back(){
		L_speed=Lsave;
		R_speed=Rsave;
	//EX0=1;EX1=1;
	L_motor_back();
	R_motor_back();
}
void car_left(){
	//EX0=0;EX1=0;
	L_speed=Lsave;
	R_speed=Rsave;
	L_motor_stop();
	R_motor_go();
}
void car_right(){
	//EX0=0;EX1=0;
	R_speed=Rsave;
	L_speed=Lsave;
	L_motor_go();
	R_motor_stop();
}
void car_oleft(){
	//EX0=0;EX1=0;
	L_motor_back();
	R_motor_go();
}
void car_oright(){
	//EX0=0;EX1=0;;
	L_motor_go();
	R_motor_back();
}
void car_stop(){
	//EX0=0;EX1=0;
	L_speed=Lsave;
	R_speed=Rsave;
	L_motor_stop();
	R_motor_stop();
}
void car_sleft(){
	//EX0=0;EX1=0;
	L_speed/=2;	
	L_motor_go();
	R_motor_go();
	
}
void car_sright(){
	//EX0=0;EX1=0;
	L_speed=Lsave;
  R_speed/=2;
	L_motor_go();
	R_motor_go();
}
	
void Timer0_Routine() interrupt 1
{
	TL0 = 0xAE;		//频率0.1ms
	TH0 = 0xFB;	
	T0Count++;
	T0Count%=100;
	if(T0Count<L_speed){
		ENA =1;	
	}
	else{
		ENA =0;
	}
	if(T0Count<R_speed){
		ENB =1;	
	}
	else{
		ENB =0;
	}
	
}

	void int1(void) interrupt 2		//右轮测速码盘
{
	Rcount++;
}
		
	
	
	void int0(void) interrupt 0		//左轮		
{
	Lcount++;
	if(Lcount>=20){
		EX0=0;EX1=0;
		CHA=0;
		CHA = Lcount-Rcount;
		Lcount=0;
		Rcount=0;
		if(R_speed-L_speed<=6)
			{
		R_speed+=CHA;
		Rsave=R_speed;
		}
		EX0=1;EX1=1;

	}
}


